import { login, getUserInfo } from "@/api/sys";
import md5 from "md5";
import { getItem, removeAllItem, removeItem, setItem } from "@/utils/storage";
import { TOKEN } from '@/constant'
import router from "@/router";
import { setTimeStamp } from "@/utils/auth";


export default {
    namespaced: true,
    state: {
        token: getItem(TOKEN) || '',
        userInfo: {}
    },
    mutations: {
        setToken(state, token) {
            state.token = token
            setItem(TOKEN, token)
        },
        setUserInfo(state, userInfo){
            state.userInfo = userInfo
        }
    },
    actions: {
        login(context, userInfo) {
           const { username, password } = userInfo
           return new Promise((resolve, reject) => {
                // sys中导入的login方法
                login({
                    username,
                    password: md5(password)
                }).then(data => {
                    // console.log(data)
                    // 提交mutation，保存token
                    this.commit('user/setToken',data.token)
                    // 保存登录时间
                    setTimeStamp()
                    resolve()
                }).catch(err => {
                    reject(err)
                })
           }) 
        },
        async getUserInfo(context){
            const res = await getUserInfo()
            console.log('res',res)
            this.commit('user/setUserInfo', res)
            // this.commit('user/setUserInfo','123')
            return res
            
        },
        logout() {
            this.commit('user/setToken','')
            this.commit('user/setUserInfo',{})
            removeAllItem()
            router.push('/login')
        }
    }
}